function xToOptim=transToOptim(theta,index,lowerBound,upperBound) 
% transToOptim
% 
% function xToOptim=transToOptim(theta,index,lowerBound,upperBound) 
% 
% Transforms the original vector THETA for optimization 
% 
%% Inputs 
% *theta*  Vector of coefficients as used in the model 
% *index*  if =0 not transformed 
%          if =1 bounded (a,b)          Must define Lower and Upper Bound
%          if =2 bounded (0,inf) 
%          if =3 bounded (0,1) 
% 
% *lowerBound* a: Only needed if index=1 
% *upperBound* b  Only needed if index=1 
%
%% Output
% xToOptim: coefficient to be used in the optimization 
% 
Ntheta=length(theta(:)); 
if Ntheta~= length(index(:) ) 
    error('Size mistmatch THETA and INDEX')
end 
if Ntheta~= length(lowerBound(:) ) 
    error('Size mistmatch THETA and lowerBound')
end 
if Ntheta~= length(upperBound(:) ) 
    error('Size mistmatch THETA and upperBound')
end 
xToOptim=zeros(Ntheta,1); 
for ii=1:Ntheta
    switch index(ii)         
        case 0 
            xToOptim(ii)=theta(ii);         
        case 1 
            tempCX=2*( theta(ii) - 0.5*(lowerBound(ii)+upperBound(ii)) )...
                / ( upperBound(ii)-lowerBound(ii) );         
            xToOptim(ii)=tempCX/sqrt( 1-(tempCX^2)  );
        case 2 
            xToOptim(ii)=log( theta(ii) );                         
        case 3
           xToOptim(ii)=log( theta(ii)/(1-theta(ii)) );                         
    end                 
end     
%% Check if any are Imaginary, INF or NAN 

